package dao;

import java.util.LinkedList;
import java.util.List;

import relations.SGBelongsTo;

public class SGBelongsToDAO 
{
	private static final SGBelongsToDAO instance = new SGBelongsToDAO();
	private List<SGBelongsTo> belongsto;
	
	private SGBelongsToDAO(){
		belongsto = new LinkedList<SGBelongsTo>();
		SGBelongsTo cus = null;
		
		cus = belongsto.newInstance("jaime", "Jaime", "Vazquez", "ja@j.com", 1L, "test", "kl", "1", "00778", "PR", "Gurabo", "USA");
		belongsto.add(cus);
		cus = belongsto.newInstance("jaime1", "Jaime", "Vazquez", "ja@j.com", 2L, "test", "kl", "1", "00778", "PR", "Gurabo", "USA");
		belongsto.add(cus);
	}
	
	public static SGBelongsToDAO getInstance() {
		return instance;
	}

	public synchronized SGBelongsTo findSGBelongsTo(Long group_id, Long single_id) {
		SGBelongsTo result = null;
		
		for (SGBelongsTo s : belongsto){
			if (s.getGroup_id().equals(group_id) && s.getSingle_id().equals(single_id)){
				result = s;
			}
		}
		return result;
	}

	public synchronized void addSGBelongsTo(SGBelongsTo obj) throws RecordDuplicateException{
		if (findSGBelongsTo(obj.getGroup_id(), obj.getSingle_id()) != null){
			throw new RecordDuplicateException("SGBelongsTo with group id: " + obj.getGroup_id()
					+ ", and single id: " + obj.getSingle_id() + " is already in the db.");
		}
		else {
			belongsto.add(obj);
		}
	}
}
